/*
 * =============================================================================
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <eirikb@google.com> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return Eirik Brandtzæg
 * =============================================================================
 */
package no.eirikb.robotec.terminal.element;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import no.eirikb.robotec.telnet.TelnetCommands;
import no.eirikb.robotec.terminal.Terminal;

/**
 *
 * @author eirikb
 * @author <a href="mailto:eirikb@google.com">eirikb@google.com</a>
 */
public class Less extends Element {

    private List<String> lines;
    private int x;
    private int y;

    public Less(Terminal terminal, int x, int y, int width, int height, String fileName) {
        super(terminal, x, y, width, height);

        BufferedReader reader = null;
        try {
            lines = new ArrayList<String>();
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(
                    terminal.getFile().getAbsolutePath() + File.separator + fileName)));
            String line = "";
            while ((line = reader.readLine()) != null) {
                lines.add(line);
            }
        } catch (IOException ex) {
            Logger.getLogger(Less.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                reader.close();
            } catch (IOException ex) {
                Logger.getLogger(Less.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        x = 0;
        y = 0;
    }

    @Override
    public void init() {
        printFile(0);
    }

    private void printFile(int lastLagLine) {
        getTerminal().getServer().sendText(
                TelnetCommands.CLEARRECT(getX(), getY(), getWidth(), getHeight()));
        String fullPrint = "";
        for (int i = y; i < lines.size(); i++) {
            if (i - y < getHeight()) {
                String line = lines.get(i);
                if (line.length() > x) {
                    line = line.substring(x);
                }
                if (line.length() > getWidth()) {
                    line = line.substring(0, getWidth());
                }
                String toSend = TelnetCommands.MOVECURSOR(getX(), getY() + i - y + 1) + line;
                if (i - y >= lastLagLine) {
                    if (fullPrint.length() > 0) {
                        getTerminal().getServer().sendText(fullPrint);
                        fullPrint = "";
                    }
                    getTerminal().getServer().sendLagText(toSend);
                } else {
                    fullPrint += toSend;
                }
            } else {
                i = lines.size();
                break;
            }
        }
        if (fullPrint.length() > 0) {
            getTerminal().getServer().sendText(fullPrint);
        }
    }

    @Override
    public void read(int c) {
        switch (c) {
            // LEFT
            case 68:
                x--;
                if (x < 0) {
                    x = 0;
                }
                printFile(getHeight());
                break;
            // UP
            case 65:
                y--;
                if (y < 0) {
                    y = 0;
                    getTerminal().prevElement();
                }
                printFile(getHeight());
                break;
            //RIGHT
            case 67:
                x++;
                printFile(getHeight());
                break;
            //DOWN
            case 66:
                y++;
                if (y > lines.size() - getHeight()) {
                    y = lines.size() - getHeight();
                    getTerminal().nextElement();
                }
                printFile(getHeight() - 1);
                break;
        }
    }
}
